package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("{id}")
    public ResponseEntity<CategoryDTO> findCategoryById(@PathVariable("id")Long id){
        return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
    }

    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> findCategoryByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.listByIds(ids)));
    }

    @GetMapping("of/parent")
    public ResponseEntity<List<CategoryDTO>> findCategoryByParentId(@RequestParam("pid")Long pid){
        return ResponseEntity.ok(CategoryDTO.convertEntityList(
           categoryService.query().eq("parent_id",pid).list()
        ));
    }

    @GetMapping("of/brand")
    public ResponseEntity<List<CategoryDTO>> findCategoryByBrandId(@RequestParam("id")Long bid){
        return ResponseEntity.ok(categoryService.findCategoryByBrandId(bid));
    }
}
